package heliotrope.net.loader
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	
	[Event(name="complete",type="flash.events.Event")]
	[Event(name="progress",type="flash.events.ProgressEvent")]
	[Event(name="ioError",  type="flash.events.IOErrorEvent")]
	public class CommonLoader extends EventDispatcher
	{
		private var _imgLoader:ImgLoader;
		private var _dataLoader:DataLoader;
		private var _soundLoader:SoundLoader;
		private var _currentLoader:ILoader;
		private var _id:uint;
		private var _isLoading:Boolean;
		private var _currentMission:LoadMission;

		public function CommonLoader(id_:uint = 0)
		{
			_id = id_;
			_imgLoader = new ImgLoader;
			_dataLoader = new DataLoader;
			_soundLoader = new SoundLoader;
		}
		
		public function load(mission_:LoadMission):void
		{
			_currentMission = mission_;
			var type:String = mission_.loadType;
			switch(type)
			{
				case LoadType.PIC:
				{
					addListeners(_imgLoader);
					_imgLoader.load(mission_.url, mission_.target);
					break;
				}
				case LoadType.SOUND:
				{
					addListeners(_soundLoader);
					_soundLoader.load(mission_.url, mission_.target);
					break;
				}	
				default:
				{
					addListeners(_dataLoader);
					_dataLoader.load(mission_.url, mission_.target, mission_.loadType);
					break;
				}
			}
			_isLoading = true;
		}
		
		protected function addListeners(loader_:ILoader):void
		{
			_currentLoader = loader_;
			loader_.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			loader_.addEventListener(Event.COMPLETE,completeHandler);
			loader_.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		protected function delListeners(loader_:ILoader):void
		{
			loader_.removeEventListener(Event.COMPLETE,completeHandler);
			loader_.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			loader_.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		protected function completeHandler(event:Event):void
		{
			dispatchEvent(new Event(Event.COMPLETE));
			//remove(); //Don't use this line.
		}
		//加载过程
		protected function progressHandler(event:ProgressEvent):void
		{
			dispatchEvent(event);
		}
		
		//错误处理
		protected function ioErrorHandler(event:IOErrorEvent):void
		{
			if(_currentMission.retry!=0)
			{
				trace("Try to reload", _currentMission);
				_currentMission.retry -= 1;
				load(_currentMission);
				return;
			}
			trace(this+' ' + event.type + " at " + this.toString()+" on "+_currentMission.url);
			dispatchEvent(event);
			remove();
		}
		
		public function unload():void
		{
			if(_currentLoader)
			{
				_currentLoader.unload();
				remove();
			}
		}
		protected function remove():void
		{
			if(_currentLoader)
			{			
				delListeners(_currentLoader);
				_currentLoader = null;
				_currentMission = null;
				_isLoading = false;
			}
		}
		
		public function get id():uint
		{
			return _id;
		}
		
		public function set id(id_:uint):void
		{
			_id = id_;
		}

		public function get currentMission():LoadMission
		{
			return _currentMission;
		}
		
		override public function toString():String
		{
			return "CommonLoader["+_id+']:';
		}
	}
}